package collection;

import java.util.ArrayList;
import java.util.List;

public class SubListDemo {
    public static void main(String[] args) {
        //List subList(int start,int end);//获取集合的子集
        List<Integer> list =new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
            list.add(i*10);
        }
        System.out.println(list);//[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

        List<Integer> subList =list.subList(3,8);
        //没有产生新集合，创建了一个新的索引指向了截取的片段
        //截取第三到第七个作为子集(含头不含尾)
        System.out.println(subList);

        for (int i = 0; i <subList.size() ; i++) {
            subList.set(i,subList.get(i)*10);
        }
        System.out.println("subList:"+subList);//subList:[300, 400, 500, 600, 700]
        //对子集的操作就是对原集合的操作
        System.out.println("list:"+list);// list:[0, 10, 20, 300, 400, 500, 600, 700, 80, 90]

        list.set(3,1000);
        //原集合变化子集也跟着变
        System.out.println("list:"+list);
        System.out.println("subList:"+subList);

        list.remove((0));
        System.out.println("list:"+list);//[10, 20, 1000, 400, 500, 600, 700, 80, 90]
        //原集合修改长度后，子集不能在操作了，运行报错，除非重新截取子集
        //System.out.println("subList:"+subList);//报错




    }
}
